﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//Author: ljj
//Date: 20190618
//Function: 用来管理BattleObject,提供目标选取等接口
public class BattleObjectManager : MonoBehaviour
{
    public static BattleObjectManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = GameObject.FindObjectOfType<BattleObjectManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("BattleObjectManager");
                _Instance = obj.AddComponent<BattleObjectManager>();
                _Instance.init();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static BattleObjectManager _Instance;

    private List<GameObject>[] lists;

    private void init()
    {
        int num = 0;
        foreach (int i in System.Enum.GetValues(typeof(Identity)))
        {
            ++num;
        }
        lists = new List<GameObject>[num];
        foreach (int i in System.Enum.GetValues(typeof(Identity)))
        {
            if (lists[i] == null)
                lists[i] = new List<GameObject>();
        }
    }

    private List<GameObject> getList(Identity identity)
    {
        if (lists == null)
            init();
        return lists[(int)identity];
    }

    public void addObject(Identity identity, GameObject obj)
    {
        List<GameObject> list = getList(identity);
        list.Add(obj);
    }

    //返回圆内的所有异类
    public List<GameObject> findOtherInCircle(Vector3 position, float radius, Identity identity)
    {
        List<GameObject> retList = new List<GameObject>();
        foreach (Identity i in System.Enum.GetValues(typeof(Identity)))
        {
            if (i == identity)
                continue;
            List<GameObject> list = getList(i);
            list.RemoveAll(x => x == null);
            for (int j = 0; j < list.Count; j++)
            {
                if (Function.DistanceIgnoreZ(list[j].transform.position, position) <= radius)
                {
                    retList.Add(list[j]);
                }
            }
        }
        return retList;
    }

    //返回圆内的所有指定类
    public List<GameObject> findInCircle(Vector3 position, float radius, List<Identity> identitys)
    {
        List<GameObject> retList = new List<GameObject>();
        foreach (Identity i in identitys)
        {
            List<GameObject> list = getList(i);
            list.RemoveAll(x => x == null);
            for (int j = 0; j < list.Count; j++)
            {
                if (Function.DistanceIgnoreZ(list[j].transform.position, position) <= radius)
                {
                    retList.Add(list[j]);
                }
            }
        }
        return retList;
    }

    //返回指定方格中的所有异类
    public List<GameObject> findOtherInLogicPos(List<Vector2Int> logicPos,Identity identity)
    {
        List<GameObject> retList = new List<GameObject>();
        foreach (Identity i in System.Enum.GetValues(typeof(Identity)))
        {
            if (i == identity)
                continue;
            List<GameObject> list = getList(identity);
            list.RemoveAll(x => x == null);
            foreach (Vector2Int pos in logicPos)
            {
                retList.Add(list.Find(x => Function.WorldPosToLogicPos(x.transform.position) == pos));
            }
        }
        return retList;
    }

    //返回指定方格中的所有指定类
    public List<GameObject> findInLogicPos(List<Vector2Int> logicPos, List<Identity> identitys)
    {
        List<GameObject> retList = new List<GameObject>();
        foreach (Identity i in identitys)
        {
            List<GameObject> list = getList(i);
            list.RemoveAll(x => x == null);
            foreach (Vector2Int pos in logicPos)
            {
                retList.Add(list.Find(x => Function.WorldPosToLogicPos(x.transform.position) == pos));
            }
        }
        return retList;
    }
}
